package com.ostrich.note.ui.note.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ostrich.note.R;
import com.ostrich.note.databinding.ItemNoteTypeBinding;
import com.ostrich.note.databinding.ItemTypeEditBinding;
import com.ostrich.note.ui.note.entity.TypeEntity;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;


public class NoteTypeEditAdapter extends RecyclerView.Adapter<NoteTypeEditAdapter.NoteTypeEditViewHolder> {
    private List<TypeEntity.ListBean> mList = new ArrayList<>();
    private ItemChildClickListener mItemChildClickListener;

    public void setItemChildClickListener(ItemChildClickListener itemChildClickListener) {
        mItemChildClickListener = itemChildClickListener;
    }

    public TypeEntity.ListBean getDataByPosition(int position){
        return mList.get(position);
    }

    //数据做刷新
    public void setData(List<TypeEntity.ListBean> list){
        if(list != null && list.size()>0){
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    //删除数据
    public void deleteData(int position){
        mList.remove(position);
        notifyDataSetChanged();
    }

    //添加数据
    public void insertData(TypeEntity.ListBean listBean){
        mList.add(listBean);
        notifyDataSetChanged();
    }
    //添加数据
    public void updateData(int position,String title){
        TypeEntity.ListBean listBean = mList.get(position);
        listBean.setTitle(title);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public NoteTypeEditViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemTypeEditBinding itemTypeEditBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_type_edit, parent, false);
        return new NoteTypeEditViewHolder(itemTypeEditBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull NoteTypeEditViewHolder holder, int position) {
        holder.mItemNoteTypeBinding.setNoteType(mList.get(position));
        holder.mItemNoteTypeBinding.ivSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mItemChildClickListener != null){
                    mItemChildClickListener.onItemChildClick(v,position);
                }
            }
        });
        holder.mItemNoteTypeBinding.ivEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mItemChildClickListener != null){
                    mItemChildClickListener.onItemChildClick(v,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class NoteTypeEditViewHolder extends RecyclerView.ViewHolder{
        private ItemTypeEditBinding mItemNoteTypeBinding;
        public NoteTypeEditViewHolder(ItemTypeEditBinding mItemNoteTypeBinding) {
            super(mItemNoteTypeBinding.getRoot());
            this.mItemNoteTypeBinding = mItemNoteTypeBinding;


        }
    }

    public interface ItemChildClickListener{
        void onItemChildClick(View view,int position);
    }




}
